<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="query">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="管理员名称" width="120" >
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="60" >
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="150" >
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="150" >
      </el-table-column>
      <el-table-column prop="state" label="员工状态" width="100" :formatter="formatSex" sortable>
        <!--scope：可以获取当前这一行的数据  -->
        <template scope="scope">
          <span style="color: red" v-if="scope.row.state === 0">禁用</span>
          <span style="color: #11b95c" v-else-if="scope.row.state === 1">启用</span>
          <span style="color: #5e7382" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="department.name" label="所属部门" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

    <!--编辑界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="管理员名" prop="username">
					<el-input v-model="saveForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" v-model="showClass" v-if="showClass==true" prop="password">
					<el-input v-model="saveForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="再次密码" v-model="showClass" v-if="showClass==true" prop="comfirmPassword">
					<el-input v-model="saveForm.comfirmPassword" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="手机号" prop="phone">
					<el-input v-model="saveForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="saveForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="saveForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="saveForm.state">
						<el-radio class="radio" :label="1">启用</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
<!--				<el-form-item label="所属部门" prop="department_id">-->
<!--					<el-input v-model="saveForm.department_id" auto-complete="off"></el-input>-->
<!--				</el-form-item>-->
        <el-form-item label="所属部门">
          <div class="block">
            <el-cascader
              v-model="saveForm.department_id"
              :options="options"
              :props="{
                checkStrictly: true,
                // 部门id
                value: 'id',
                // 部门名称
                label: 'name',
              }"
              clearable></el-cascader>
          </div>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, saveDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
        showClass:this,
        title:"",
			  // 高级查询
				filters: {
          keyword: ''
				},
        // 列表数据
				employees: [],
        // 部门树数据
        options:[],
        // 总条数
				totals: 0,
        // 当前页条数
				pageSize: 5,
        // 当前页
        currentPage:1,
        // 页面忙等
				listLoading: false,
				sels: [],//列表选中列

        //编辑界面是否显示
				saveFormVisible: false,
        // 修改忙等
				saveLoading: false,
        // 修改表单校验
				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				saveForm: {
          id:"",
          // 姓名
          username: '',
          password:'',
          comfirmPassword:'',
          // 电话
          phone: '',
          // 邮箱
          email: '',
          // 年龄
          age: '',
          // 状态
          state:0,
          // 上级部门id
          department_id:""
				},
			}
		},
		methods: {
      query() {
        // 高级查询定位第一页
        this.currentPage = 1;
        this.getEmployees();
      },
			//性别显示转换
			formatSex: function (row, column) {

			},
      // 点击页数
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getEmployees();
			},

			//获取部门列表
			getEmployees() {
			  // 携带到后端的参数
				let para = {
					pageSize: this.pageSize,
          currentPage: this.currentPage,
          keyword: this.filters.keyword
				};
				// 忙等
				this.listLoading = true;
				//NProgress.start();
				// 发送axios请求获取后端数据
        this.$http.post("/emp",para)
        .then(result=>{
          // 关闭忙等
          this.listLoading = false;
          // console.log(result);
          // 列表数据赋值
          this.employees = result.data.rows;
          // totals赋值
          this.totals = result.data.totals;
          // console.log(result.data.totals);
        })
        .catch(result=>{
          // 请求失败
          this.$message({
            message: '😥网络繁忙',
            type: 'error'
          });
        })
			},

			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
				  // 开启忙等
					this.listLoading = true;
          // 发请求后端删除
          this.$http.delete("/emp/"+row.id)
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              // 定位到第一页
              this.currentPage = 1;
              // 刷新页面
              this.getEmployees();
            })
            .catch(result=>{
              // 请求失败
              this.$message({
                message: '😥网络繁忙,删除失败',
                type: 'error'
              });
              // 定位到第一页
              this.currentPage = 1;
              // 刷新页面
              this.getEmployees();
            })
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.showClass = false;
        this.title = "编辑"
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.showClass = true;
        this.title = "新增"
				this.saveFormVisible = true;
				this.saveForm = {
          id:"",
          // 姓名
          username: '',
          password:'',
          comfirmPassword:'',
          // 电话
          phone: '',
          // 邮箱
          email: '',
          // 年龄
          age: '',
          // 状态
          state:0,
          // 上级部门id
          department_id:""
				};
			},
			//编辑
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  // 开启忙等
							this.saveLoading = true;
              let para = Object.assign({}, this.saveForm);

              if (para.department_id && para.department_id instanceof Array) {
                para.department_id = para.department_id[para.department_id.length -1];
              }

              this.$http.post("/emp/save",para)
                .then(result=>{
                  // 关闭忙等
                  this.saveLoading = false;
                  // 关闭模态框
                  this.saveFormVisible = false;
                  // 判断后端是否成功
                  if(result.data.success) {
                    // 操作成功
                    this.$message({
                      message: result.data.message,
                      type: 'success'
                    });
                    // 定位到第一页
                    this.currentPage = 1;
                    // 刷新页面
                    this.getEmployees();
                  } else {
                    // 请求失败
                    this.$message({
                      message: result.data.message,
                      type: 'error'
                    });
                    // 定位到第一页
                    this.currentPage = 1;
                    // 刷新页面
                    this.getEmployees();
                  }
                })
                .catch(result=>{
                  // 请求失败
                  this.$message({
                    message: '😥网络繁忙，我们正在抢救程序员！！！',
                    type: 'error'
                  });
                  // 定位到第一页
                  this.currentPage = 1;
                  // 刷新页面
                  this.getEmployees();
                })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          this.$http.patch("/emp/",ids)
            .then(result=>{
              // 关闭忙等
              this.listLoading = false;
              // 定位到第一页
              this.currentPage = 1;
              // 刷新页面
              this.getEmployees();
            })
            .catch(result=>{
              // 请求失败
              this.$message({
                message: '😥网络繁忙，我们正在抢救程序员！！！',
                type: 'error'
              });
              // 定位到第一页
              this.currentPage = 1;
              // 刷新页面
              this.getEmployees();
            })
				}).catch(() => {

				});
			},
      getDeptTree() {
        this.$http.get("/dept/tree")
          .then(result=>{
            this.options = result.data;
          })
          .catch(result=>{
            // 请求失败
            this.$message({
              message: '😥网络繁忙，我们正在抢救程序员！！！',
              type: 'error'
            });
          })
      },
		},
		mounted() {
			this.getEmployees();
			this.getDeptTree();
		}
	}

</script>

<style scoped>

</style>
